package com.rc.activity.sync.util;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;

import com.rc.activity.dao.DaoSession;
import com.rc.activity.dao.tbl_Trip;
import com.rc.activity.dao.tbl_TripDao;
import com.rc.net.NetSupport;
import com.rc.object.BaseObject;

public class TripSync {

	private Context context;
	private SQLiteDatabase db;
	private DaoSession daoSession;
	public String query;
	private tbl_TripDao daoTrip;
	public Cursor cursor;
	public static String[] keys = { "status", "TripID", "messages" };

	public TripSync(Context context, SQLiteDatabase db, DaoSession daoSession) {
		this.context = context;
		this.daoSession = daoSession;
		this.db = db;
		daoTrip = this.daoSession.getTbl_TripDao();
	}

	/**
	 * Delete multi Trip
	 */
	public void deleteTrip() {
		new callDeleteTrip().execute("");
	}

	/**
	 * Insert Trip
	 */
	public void insertTrip() {
		new callInsertTrip().execute("");
	}

	/**
	 * Update Trip
	 */
	public void UpdateTrip() {
		new callUpdateTrip().execute("");
	}

	/**
	 * call multidelteTrip
	 */
	private class callDeleteTrip extends
			AsyncTask<String, Void, ArrayList<Long>> {

		public callDeleteTrip() {
			super();
		}

		@Override
		protected ArrayList<Long> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_TripDao.Properties.IsDelete.columnName + " = '1'";
			ArrayList<tbl_Trip> arrTrip;
			ArrayList<Long> arrTripID = new ArrayList<Long>();

			// get data in sqlite
			cursor = db.query(daoTrip.getTablename(), daoTrip.getAllColumns(),
					query, null, null, null, null);
			arrTrip = new ArrayList<tbl_Trip>();

			// get ArrayList Trip by cursor
			arrTrip = tbl_Trip.getObjectbyCursor(cursor);
			if (arrTrip.size() > 0) {
				for (tbl_Trip objTrip : arrTrip) {
					// get ID Trip
					arrTripID.add(objTrip.getTripID());
				}
				// query server
				if (NetSupport.deleteTrips(arrTripID, context)) {
					return arrTripID;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<Long> arrTripID) {
			if (arrTripID != null) {
				insertTrip();
			}
		}
	}

	/**
	 * call multiInsertTrip
	 */
	private class callInsertTrip extends
			AsyncTask<String, Void, ArrayList<BaseObject>> {
		ArrayList<tbl_Trip> arrTrip;

		@Override
		protected ArrayList<BaseObject> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_TripDao.Properties.IsCreate.columnName + " = '1'";

			// get data in sqlite
			cursor = db.query(daoTrip.getTablename(), daoTrip.getAllColumns(),
					query, null, null, null, null);
			arrTrip = new ArrayList<tbl_Trip>();

			// get ArrayList Trip by cursor
			arrTrip = tbl_Trip.getObjectbyCursor(cursor);
			if (arrTrip.size() > 0) {
				// query server
				return NetSupport.insertTrips(arrTrip);
			}
			return null;
		}

		protected void onPostExecute(ArrayList<BaseObject> arrBase) {
			if (arrBase != null) {
				for (int i = 0; i < arrBase.size(); i++) {
					long mTrip = arrTrip.get(i).getTripID();
					if (arrBase.get(i).getParam(keys[0]).equals("success")) {
						tbl_Trip objTrip = new tbl_Trip();
						objTrip = arrTrip.get(i);
						objTrip.setTripID(arrBase.get(i).getParamLong(keys[1]));
						objTrip.setIsCreate("");
						db.update(tbl_TripDao.TABLENAME,
								objTrip.getContentValues(),
								tbl_TripDao.Properties.TripID.columnName
										+ " = " + mTrip, null);
					} else {

					}
				}

			}
			UpdateTrip();
		}
	}

	/**
	 * call update trip
	 */
	private class callUpdateTrip extends
			AsyncTask<String, Void, ArrayList<tbl_Trip>> {

		@Override
		protected ArrayList<tbl_Trip> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_TripDao.Properties.IsUpdate.columnName + " = '1'";
			ArrayList<tbl_Trip> arrTrip;

			// get data in sqlite
			cursor = db.query(daoTrip.getTablename(), daoTrip.getAllColumns(),
					query, null, null, null, null);
			arrTrip = new ArrayList<tbl_Trip>();

			// get ArrayList Trip by cursor
			arrTrip = tbl_Trip.getObjectbyCursor(cursor);
			if (arrTrip.size() > 0) {
				// query server
				if (NetSupport.updateTrips(arrTrip)) {
					return arrTrip;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Trip> arrTripID) {
			if (arrTripID != null) {
				for (tbl_Trip objTrip : arrTripID) {
					objTrip.setIsUpdate("");
					int row = db.update(daoTrip.getTablename(),
							objTrip.getContentValues(),
							tbl_TripDao.Properties.TripID.columnName + " = "
									+ objTrip.getTripID(), null);
					Log.e("update_trip_success", row + "");
				}
			}

		}
	}
}
